package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.admin.UserRequest;
import com.springboot_gradrecruit.model.dto.student.AddResumeRequest;
import com.springboot_gradrecruit.model.dto.student.PageStudentRequest;
import com.springboot_gradrecruit.model.dto.student.UpdateStudentRequest;
import com.springboot_gradrecruit.model.entity.Student;
import com.springboot_gradrecruit.model.vo.student.StudentVO;
import com.springboot_gradrecruit.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author zr
 */

@Tag(name = "学生相关接口")
@RestController
@RequestMapping("/student")
public class StudentController {


    @Resource
    private StudentService studentService;

    //增加
    @Operation(summary = "添加个人简历")
    @PostMapping("/addResume")
    public R<Boolean> addResume(@RequestBody AddResumeRequest addResumeRequest, HttpServletRequest request) {
        //1. 参数校验
        if (addResumeRequest == null || request == null) {
            throw new RuntimeException("参数为空");
        }
        //2。. 调用service完成添加
        Long aLong = studentService.addResume(addResumeRequest, request);
        if (aLong > 0) {
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");

    }

    //删除
    @Operation(summary = "根据学生的ID删除")
    @PostMapping("/deleteStudentById")
    public R<Boolean> deleteStudentById(Long studentId) {
        //1. 参数校验
        if (studentId == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成删除
        boolean deleteStudent = studentService.removeById(studentId);
        if (deleteStudent) {
            return R.success(null, "删除成功");
        }
        return R.error(null, "删除失败");
    }

    //修改
    @Operation(summary = "修改学生信息根据学生ID")
    @PostMapping("/updateStudentById")
    public R<Boolean> updateStudentById(@RequestBody UpdateStudentRequest updateStudentRequest, HttpServletRequest request) {
        //1. 参数校验
        if (updateStudentRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        boolean updateStudent = studentService.updateStudentById(updateStudentRequest);
        if (updateStudent) {
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }

    //查询
    @Operation(summary = "查询简历根据学生绑定的用户ID")
    @PostMapping("/queryResumeByStudentUserId")
    public R<StudentVO> queryResumeByStudentUserId(Long studentUserId) {
        //1. 参数校验
        if (studentUserId == null) {
            throw new RuntimeException("参数为空");
        }
        //2。. 调用service完成查询
        StudentVO studentVO = studentService.queryResumeByStudentUserId(studentUserId);
        return R.success(studentVO, "查询成功");

    }

    @Operation(summary = "分页查询学生信息")
    @PostMapping("/pageStudent")
    public R<PageResponse<StudentVO>> pageStudent(@RequestBody PageStudentRequest pageStudentRequest, HttpServletRequest request) {
        //1. 参数校验
        if (pageStudentRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        PageResponse<StudentVO> pageStudent = studentService.pageStudent(pageStudentRequest);
        return R.success(pageStudent, "查询成功");
    }

    @Operation(summary = "查询学生信息根据学生ID")
    @PostMapping("/queryStudentById")
    public R<StudentVO> queryStudentById(Long studentId) {
        //1. 参数校验
        if (studentId == null) {
            throw new RuntimeException("参数为空");
        }
        //2. 调用service完成查询
        Student student = studentService.getById(studentId);
        StudentVO studentVO = BeanUtil.copyProperties(student, StudentVO.class);
        return R.success(studentVO, "查询成功");
    }
}
